﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.Script.Serialization;

public class DynamicJsonConverter : JavaScriptConverter
{
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary == null)
        {
            throw new ArgumentNullException("dictionary");
        }
        return ((type == typeof(object)) ? new DynamicJsonObject(dictionary) : null);
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable<Type> SupportedTypes
    {
        get
        {
            return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) }));
        }
    }

    private sealed class DynamicJsonObject : DynamicObject
    {
        private readonly IDictionary<string, object> _dictionary;

        public DynamicJsonObject(IDictionary<string, object> dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }
            this._dictionary = dictionary;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("{");
            this.ToString(sb);
            return sb.ToString();
        }

        private void ToString(StringBuilder sb)
        {
            bool flag = true;
            foreach (KeyValuePair<string, object> pair in this._dictionary)
            {
                if (!flag)
                {
                    sb.Append(",");
                }
                flag = false;
                object obj2 = pair.Value;
                string key = pair.Key;
                if (obj2 is string)
                {
                    sb.AppendFormat("{0}:\"{1}\"", key, obj2);
                }
                else if (obj2 is IDictionary<string, object>)
                {
                    new DynamicJsonConverter.DynamicJsonObject((IDictionary<string, object>) obj2).ToString(sb);
                }
                else if (obj2 is ArrayList)
                {
                    sb.Append(key + ":[");
                    bool flag2 = true;
                    foreach (object obj3 in (ArrayList) obj2)
                    {
                        if (!flag2)
                        {
                            sb.Append(",");
                        }
                        flag2 = false;
                        if (obj3 is IDictionary<string, object>)
                        {
                            new DynamicJsonConverter.DynamicJsonObject((IDictionary<string, object>) obj3).ToString(sb);
                        }
                        else if (obj3 is string)
                        {
                            sb.AppendFormat("\"{0}\"", obj3);
                        }
                        else
                        {
                            sb.AppendFormat("{0}", obj3);
                        }
                    }
                    sb.Append("]");
                }
                else
                {
                    sb.AppendFormat("{0}:{1}", key, obj2);
                }
            }
            sb.Append("}");
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!this._dictionary.TryGetValue(binder.Name, out result))
            {
                result = null;
                return true;
            }
            IDictionary<string, object> dictionary = result as IDictionary<string, object>;
            if (dictionary != null)
            {
                result = new DynamicJsonConverter.DynamicJsonObject(dictionary);
                return true;
            }
            ArrayList source = result as ArrayList;
            if ((source != null) && (source.Count > 0))
            {
                if (source[0] is IDictionary<string, object>)
                {
                    result = new List<object>(from x in source.Cast<IDictionary<string, object>>() select new DynamicJsonConverter.DynamicJsonObject(x));
                }
                else
                {
                    result = new List<object>(source.Cast<object>());
                }
            }
            return true;
        }
    }
}

